<?php
namespace Api\Controller;
use Common\Controller\ApiController;
use Common\ORG\PublicFunction;
use Common\ORG\WxFunction;
use Think\Db;


/**

 * 企业信息

 *

 * 企业信息控制类。

 * @Author      LD(191696242@qq.com)

 * @Version     1.o

 * @Date        2020/12/30

 * @Time        16:00

 * @since

 */
class CompanyNewController extends ApiController{

    private $company = [];
    private $date   = '';
    public function _initialize()
    {
        parent::_initialize();
        if(!IS_POST){
            $this->ajaxReturn(1, '提交方式错误！');
        }

        /*if($this->userinfo['utype']<>1){
            $this->ajaxReturn(1, '当前账户不是企业用户！');
        }*/
        $this->company  = $this->isCompany();
        $this->date     = date('Y-m');
        
    }

    /**
     * comInfo
     * 企业资料添加

     * 添加企业基础信息资料

     * @access public

     * @param string    $companyname (必填：是) 公司名称
     * @param string    $certificate_img (必填：是) 证件上传地址
     * @param int       $trade (必填：是) 工程类别 cn
     * @param string    $district (必填：是) 公司所在地 cn
     * @param string    $address (必填：是) 详细地址
     * @param int       $nature (必填：是)公司性质 cn
     * @param int       $scale (必填：是) 公司规模 cn
     * @param string    $registerDate (必填：是) 企业成立时间
     *
     * @param string    $contents (必填：是) 企业简介
     * @param string    $contact (必填：是) 联系人
     * @param string    $telephone (必填：是) 手机号
     * @param string    $email (必填：是) 邮箱
     * @param string    $mapx (必填：是) 地图X坐标
     * @param string    $mapy (必填：是) 地图y坐标
     * @param string    $imgs (必填：是) 图片

     * @since

     * @return json

     */
    public function comInfo(){
        $ints       = array('nature', 'scale');
        $trims      = array('trade', 'companyname', 'certificate_img', 'logo', 'imgs', 'district', 'address', 'register_date', 'contents', 'contact', 'telephone', 'email', 'map_x', 'map_y');
        $category   = D('Category')->get_category_cache();//全部单一分类
        $trade      = D('CategoryMajor')->get_major_list();//工程分类

        $companyProfile  =$this->isCompany();
        foreach ($ints as $val) {
            $setSqlArr[$val] = I('post.' . $val, 0, 'intval');
        }
        foreach ($trims as $val) {
            $setSqlArr[$val] = I('post.' . $val, '', 'trim,badword');
        }
        if($companyProfile){
            $setSqlArr['id']    = $companyProfile['id'];
        }
//        $setSqlArr['def']   = 1;
//        $setSqlArr['stime'] = time();
        $setSqlArr['companyname'] = !empty($companyProfile['audit']) &&  $companyProfile['audit']== 1 ? $companyProfile['companyname'] : $setSqlArr['companyname'];
        if(empty($setSqlArr['companyname'])) $this->ajaxReturn(1, '企业名称不能为空');

     //   if(empty($setSqlArr['trade']) || empty($trade[$setSqlArr['trade']])) $this->ajaxReturn(1, '工程类别不能为空');
        if(empty($setSqlArr['trade'])) $this->ajaxReturn(1, '工程类别不能为空');
        $tarade_ids=explode(',',$setSqlArr['trade']);

        $trade_cn   = [];
        foreach($tarade_ids as $k=>$v){
            $tradeArr   = explode('.', $v);
            $tnum       = count($tradeArr);
            $tid        = !empty($tradeArr[$tnum - 1]) ? $tradeArr[$tnum - 1] : $tradeArr[$tnum - 2];
            $trade_cn [] = $trade[$tid]['categoryname'];
        }
        $setSqlArr['trade_cn']  = implode(',',$trade_cn);

        if(empty($setSqlArr['district'])) $this->ajaxReturn(1, '公司所在地不能为空');

        $dizhi  = explode('.', $setSqlArr['district']);
        $dzId   = !empty($dizhi[2]) ? $dizhi[2] : (!empty($dizhi[1]) ? $dizhi[1] : $dizhi[0]);
        $city   = get_city_info($dzId);
        if(empty($city['district'])) $this->ajaxReturn(1, '公司所在地不存在');

        $setSqlArr['district_cn']   = $city['district_cn_all'];

        if(empty($setSqlArr['address'])) $this->ajaxReturn(1, '详细地址不能为空');

        if(empty($setSqlArr['nature']) || empty($category['QS_company_type'][$setSqlArr['nature']])) $this->ajaxReturn(1, '公司性质参数有误');
        $setSqlArr['nature_cn'] = $category['QS_company_type'][$setSqlArr['nature']];

        if(empty($setSqlArr['scale']) || empty($category['QS_scale'][$setSqlArr['scale']])) $this->ajaxReturn(1, '公司规模参数有误');
        $setSqlArr['scale_cn'] = $category['QS_scale'][$setSqlArr['scale']];

        if(empty($setSqlArr['register_date']) || !fieldRegex($setSqlArr['register_date'],'ym')) $this->ajaxReturn(1, '公司成立时间参数有误');

        if(empty($setSqlArr['contents'])) $this->ajaxReturn(1, '公司简介不能为空');

        if(empty($setSqlArr['contact'])) $this->ajaxReturn(1, '联系人不能为空');

        if(empty($setSqlArr['telephone']) || !fieldRegex($setSqlArr['telephone'],'mobile')) $this->ajaxReturn(1, '手机号不正确');

        if(empty($setSqlArr['email']) || !fieldRegex($setSqlArr['email'],'email')) $this->ajaxReturn(1, '邮箱地址格式不正确');

        if(empty($setSqlArr['map_x'])) $this->ajaxReturn(1, '地图X坐标不正确');

        if(empty($setSqlArr['map_y'])) $this->ajaxReturn(1, '邮箱地址格式不正确');

        if(empty($setSqlArr['logo'])) unset($setSqlArr['logo']);

        // 判断企业名称是否重复
        if (C('qscms_company_repeat') == "0") {
            $info = M('CompanyProfile')->where(array('uid' => array('neq', C('visitor.uid')), 'companyname' => $setSqlArr['companyname']))->getField('uid');
            if ($info) $this->ajaxReturn(1, "{$setSqlArr['companyname']}已经存在，不能重复注册");
        }

        if($setSqlArr['id']){
            $info = M('CompanyProfile')->where(['uid' => C('visitor.uid'), 'id' => $setSqlArr['id']])->find();
            if (!$info) $this->ajaxReturn(1, "修改的信息不存在");
        }

        $setSqlArr['audit'] = C('qscms_audit_edit_com') <> "-1" ? C('qscms_audit_edit_com') : (!empty($companyProfile['audit']) ? $companyProfile['audit'] : 2);

        $setSqlArr['uid']   = $this->userinfo['uid'];
        $res = D('CompanyProfile')->add_wx_company_profile($setSqlArr, C('visitor'));



        if($res['status'] <> 0){
            $this->ajaxReturn(1, $res['error']);
        }

        //添加公司照片集
        if(!$companyProfile) {
            if (!empty($res['id']) && !empty($setSqlArr['imgs'])) {
                //$imgs   = explode(',', $setSqlArr['imgs']);
                foreach ($setSqlArr['imgs'] as $val) {
                    $data['uid'] = C('visitor.uid');
                    $data['company_id'] = $res['id'];
                    $data['img'] = $val;
                    if (!D('CompanyImg')->where($data)->find()) {
                        D('CompanyImg')->add_company_img($data, C('visitor'));
                    }
                }
            }
        }

        $this->ajaxReturn(0, $res['error']);
    }



    /**
     * getCompany
     * 获取企业资料

     * 获取用户基础信息资料

     * @access public

     * @since

     * @return json

     */
    public function getCompany(){
        $company    = $this->company;
        $trade      = D('CategoryMajor')->get_major_list();//工程类别
        $jobTag     = D('Category')->get_category_cache()['QS_jobtag'];//公司规模

        if(empty($company)){
            $this->ajaxReturn(1, '企业信息不存在！');
        }

        //获取上传图片
        $imgsFull   = M("CompanyImg")->where(['uid'=>$this->userinfo['uid'], 'company_id'=> $company['id'], 'audit'=>['in', [1,2]]])->select();
        foreach($imgsFull as &$item){
            $item['path_full']  = C('qscms_site_domain') . __ROOT__ . '/data/upload/company_img/'.$item['img'];
            $item['addtime']    = date('Y-m-d H:i:s', $item['addtime']);
            unset($item['title'],$item['audit']);
        }

        $data   = [
            'id'            => $company['id'],
            'uid'           => $company['uid'],
            'companyname'   => $company['companyname'], //企业名称
            'nature'        => $company['nature'],//企业性质
            'nature_cn'     => $company['nature_cn'],//企业性质明文
            'trade'         => $company['trade'],//工程类别
            'trade_cn'      => explode(',', $company['trade_cn']),//工程类别明文
            'district'      => $company['district'],//公司所在地
            'district_cn'   => explode('/', $company['district_cn']),//公司所在地明文
            'scale'         => $company['scale'],//公司规模
            'scale_cn'      => $company['scale_cn'],//公司规模明文
            'address'       => $company['address'],//详细地址
            'contact'       => $company['contact'],//联系人
            'telephone'     => $company['telephone'],//联系电话
            'email'         => $company['email'],//邮件地址
            //'certificate_img'=> $company['certificate_img'] ? C('qscms_site_domain') . __ROOT__ . '/data/upload/certificate_img/'.$company['certificate_img'] : '',//企业证书
            //'logo'          => $company['logo'] ? C('qscms_site_domain') . __ROOT__ . '/data/upload/company_logo/'.$company['logo'] : '',//公司logo
            'certificate_img'=> $company['certificate_img'] ? $company['certificate_img'] : '',//企业证书
            'logo'          => $company['logo'] ? $company['logo'] : '',//公司logo
            'logo_full'     => $company['logo'] ? attach($company['logo'], 'company_logo') : attach('default.png', 'company_logo'),//公司logo
            'contents'      => $company['contents'],//企业介绍
            'audit'         => $company['audit'],//审核状态 1审核通过 2等待审核 3审核未通过
            'map_x'         => $company['map_x'],//地图经度
            'map_y'         => $company['map_y'],//地图纬度
            'register_date' => $company['register_date'],//企业成立时间
            'imgs'          => $imgsFull,//上传图片集合
            'refreshtime'   => date('Y-m-d H:i:s', $company['refreshtime']),//刷新时间
            'addtime'       => date('Y-m-d H:i:s', $company['addtime']),//添加时间


        ];

        $this->ajaxReturn(0, 'ok', $data);

    }


    /**
     * refreshtime
     * 刷新企业时间

     * 刷新企业信息时间

     * @access public

     * @param string

     * @since

     * @return json

     */
    public function refreshtime(){
        $setSqlArr['refreshtime'] = time();
        $res = D('CompanyProfile')->admin_refresh_company( $this->userinfo['uid']);

        if(!$res){
            $this->ajaxReturn(1, '刷新失败');
        }

        $this->ajaxReturn(0, '刷新成功');

    }

    /**
     * imgDel
     * 删除用户照片集上传

     * 删除当前用户的资格证书

     * @access public

     * @param string    $id (必填：是)

     * @since

     * @return json

     */
    public function imgDel(){
        $id         = I('post.id', 0, 'intval');
        if(empty($id)){
            $this->ajaxReturn(1, '删除ID不能为空');
        }
        $info  = M('CompanyImg')->where(['id'=>$id, 'uid'=> $this->userinfo['uid'], 'company_id'=>$this->company['id']])->find();
        if(empty($info)){
            $this->ajaxReturn(1, '删除的记录不存在');
        }

        if(M('CompanyImg')->delete($id)){
            $this->ajaxReturn(0, '删除成功');
        }

        $this->ajaxReturn(1, '删除失败');
    }

}
?>